package com.rpay.service.account.impl;

import com.rpay.common.dto.account.AccountDTO;
import com.rpay.common.dto.account.AccountQueryDTO;
import com.rpay.dao.account.AccountDao;
import com.rpay.model.account.AccountDO;
import com.rpay.model.account.AccountQueryDO;
import com.rpay.service.account.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XXX
 * @mail XXX@hotmail.com
 * @date 2018/5/14
 * @Description:
 */
@Service
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private AccountDao accountDao;

    @Override
    public AccountQueryDTO list(AccountQueryDTO accountQueryDTO, Integer page, Integer pageSize) {
        AccountQueryDO accountQueryDO = convertQueryDTOToDO(accountQueryDTO);
        List<AccountDO> accountDOList = accountDao.list(accountQueryDO);
        List<AccountDTO> list = new ArrayList<>();
        if (accountDOList != null && accountDOList.size() > 0) {
            for (AccountDO accountDO : accountDOList) {
                AccountDTO accountDTO = convertDOToDTO(accountDO);
                list.add(accountDTO);
            }
        }
        Integer count = accountDao.selectRow(accountQueryDO);
        AccountQueryDTO data = convertQueryDOToDTO(list, page, pageSize, count);
        return data;
    }

    @Override
    public boolean patch(AccountDTO accountDTO) {
        AccountDO accountDO = convertDTOToDO(accountDTO);
        accountDao.patch(accountDO);
        return true;
    }

    private AccountQueryDO convertQueryDTOToDO(AccountQueryDTO accountQueryDTO) {
        AccountQueryDO accountQueryDO = null;
        if (accountQueryDTO != null) {
            accountQueryDO = new AccountQueryDO();
            accountQueryDO.setPage(accountQueryDTO.getPage());
            accountQueryDO.setPageSize(accountQueryDTO.getPageSize());
            accountQueryDO.setKeyword(accountQueryDTO.getKeyword());
            accountQueryDO.setLevel(accountQueryDTO.getLevel());
            accountQueryDO.setStartTime(accountQueryDTO.getStartTime());
            accountQueryDO.setEndTime(accountQueryDTO.getEndTime());
        }
        return accountQueryDO;
    }

    private AccountDTO convertDOToDTO(AccountDO accountDO) {
        AccountDTO accountDTO = null;
        if (accountDO != null) {
            accountDTO = new AccountDTO();
            accountDTO.setId(accountDO.getId());
            accountDTO.setName(accountDO.getName());
            accountDTO.setLevel(accountDO.getLevel());
            accountDTO.setEmail(accountDO.getEmail());
            accountDTO.setPhone(accountDO.getPhone());
//            accountDTO.setIdcard(accountDO.getIdcard());
            accountDTO.setRippleWalletAddr(accountDO.getRippleWalletAddr());
            accountDTO.setRippleWalletQrcode(accountDO.getRippleWalletQrcode());
            accountDTO.setRpayWalletAddr(accountDO.getRpayWalletAddr());
            accountDTO.setRpayWalletQrcode(accountDO.getRpayWalletQrcode());
            accountDTO.setShareCode(accountDO.getShareCode());
            accountDTO.setParentId(accountDO.getParentId());
            accountDTO.setStatus(accountDO.getStatus());
            accountDTO.setCreateTime(accountDO.getCreateTime());
            accountDTO.setUpdateTime(accountDO.getUpdateTime());
        }
        return accountDTO;
    }

    private AccountQueryDTO convertQueryDOToDTO(List list, Integer page, Integer pageSize, Integer count) {
        AccountQueryDTO accountQueryDTO = new AccountQueryDTO();
        accountQueryDTO.setList(list);
        accountQueryDTO.setPage(page);
        accountQueryDTO.setPageSize(pageSize);
        accountQueryDTO.setTotalRow(count);
        return accountQueryDTO;
    }

    private AccountDO convertDTOToDO(AccountDTO accountDTO) {
        AccountDO accountDO = null;
        if (accountDTO != null) {
            accountDO = new AccountDO();
            accountDO.setId(accountDTO.getId());
            accountDO.setStatus(accountDTO.getStatus());
            accountDO.setLevel(accountDTO.getLevel());
            accountDO.setName(accountDTO.getName());
            accountDO.setPhone(accountDTO.getPhone());
            accountDO.setRippleWalletAddr(accountDTO.getRippleWalletAddr());
            accountDO.setRpayWalletAddr(accountDTO.getRpayWalletAddr());
        }
        return accountDO;
    }
}
